package ControllerSrevlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import MYJDBC.jdbc;

/**
 * Servlet implementation class SelectVideo
 */
@WebServlet("/SelectVideo")
public class SelectVideo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SelectVideo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int spage = 0;
		int num = 0;
		request.setCharacterEncoding("UTF-8");
		String selectvideo = request.getParameter("selectvideo");
		jdbc jc = new jdbc();
		ResultSet rs = jc.SqlRs("select * from video_table where videoname like '%"+selectvideo+"%' and videoaudit = "+0+" order by videoid desc");
		
		
		String pageindexs = request.getParameter("pageindexs");
		
		
		String spages = request.getParameter("spages");
		if(spages != null) {
			spage = Integer.parseInt(spages);
		}
		
		int pageindex = 0;
		if(pageindexs == null) {
			pageindex = 1;
		}else {
			pageindex = Integer.parseInt(pageindexs);
		}
		
		try {
			while(rs.next()) {
				num++;
			}
			jc.QueryClose();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		int page = num/6;
		if(num <= 6) {
			page = 1;
		}
		else{
			if(num%6 != 0) {
				page++;
			}
		}

		
		request.setAttribute("spages", spage);
		request.setAttribute("page", page);
		request.setAttribute("pageindex", pageindex);
		request.setAttribute("selectvideo", selectvideo);
		request.setAttribute("videonum", num);
		request.setAttribute("SqlUser",jc.getSqlUser());
		request.setAttribute("SqlPass",jc.getSqlPass());
		request.setAttribute("SqlName", jc.getSqlName());
		request.getRequestDispatcher("SelectVideo.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
	}

}
